package com.android.fragmentdemo.regrex

import org.junit.Test
import java.util.regex.Pattern

class TestRegex {
    @Test
    fun testMatchWord() {
        //在 Java 字符串中，\b 需要写成 \\b，因为 \ 是转义字符
        //\b 匹配一个单词的边界，即单词的开始或结束位置。
        val regex = "\\bdog[0-9]+\\b"
        val input = "dog1234 dodad dad dada"
        val pattern = Pattern.compile(regex)
        val matcher = pattern.matcher(input)
        println("start start start")
        while (matcher.find()) {
            println("findTarget:${matcher.group()}")
        }
        println("end end end")
    }

    @Test
    fun testMatchMacAddress() {
        //A0-D0-c9-50-u0-dd
        //B0:D0:c9:50:u0:dd
        val regex = "[A-Za-z0-9]{2}([-:][A-Za-z0-9]{2}){5}"
        val input = "dog B0:D0:c9:50:u0:dd dad A0-D0-c9-50-u0-dp"
        val pattern = Pattern.compile(regex)
        val matcher = pattern.matcher(input)
        println("start start start")
        while (matcher.find()) {
            println("findTarget:${matcher.group()}")
        }
        println("end end end")
    }

    @Test
    fun testAtLeastMatch() {
        //val content = "Arrwe"
        //val content = "ArrrrWRR"
        //val content = "ArYUUp"
        //val content = "Ar"
        //val content = "Arh"
        //val content = "ArhPFGG"
        //val content = "ArhP12"
        //val content = "12"
        //val content = "12a"
        //val content = "RBbta"
        // val content = "AAAAAAAAAa12"
        //val content = "rA1aAAC00b"
        //val content = "aDAAAAAAAr"
        //val content = "bDAAAAAAAr"
        //val content = "bDAAaaAAAr"
        //val content = "AAAAAAARrr"
        //val content = "aAArAAARRr"
        //val content = "AaAaAaAaaa"
        val content = "aAAAAAAAAa"

        //.表示任意字符
        //+表示至少1 ?表示0或1 *表示0或多 {n,}表示至少n {m,n}表示m-n之间
        //?=(正向前瞻)用于检查某个模式是否紧随其后，但不消耗字符
        //?!(负向前瞻)确保后面的字符不匹配，但不消耗字符
        //?<=(正向后瞻)确保前面的字符匹配，但不消耗字符
        //?<!(负向后瞻)确保前面的字符不匹配，但不消耗字符

        /*
        * String text = "foobar";
          Pattern pattern = Pattern.compile("foo(?=bar)");
          Matcher matcher = pattern.matcher(text);

          if (matcher.find()) {
             System.out.println("Match found: " + matcher.group());
          } else {
            System.out.println("No match found.");
          }
          输出：Match found: foo
          解释：正则表达式 foo(?=bar) 匹配 foo，但要求 foo 后面必须是 bar，且 bar 不会被包含在匹配结果中。
        * */


        //至少一个小写字母 一个大写字母 总共十个字符
        val regex0 = "^(?=.*[a-z])(?=.*[A-Z]).+$"

        //至少一个小写字母 一个大写字母 全是字母
        //val regex0 = "^(?=.*[a-z])(?=.*[A-Z])[a-zA-z]+$"

        //至少两个小写字母 两个大写字母 总共10个字母
        //val regex1 = "^(?=.*[a-z]{2,})(?=.*[A-Z]{2,})[a-zA-Z]{10}$"   //连续两个小写或者大写
        //val regex1 = "^(?=.*[a-z]{1,2})(?=.*[A-Z]{1,2})[a-zA-Z]{10}$" //可连续也可不连续
        //val regex1 = "^(?=.*[a-z]){1,2}(?=.*[A-Z]){1,2}[a-zA-Z]{10}$" //可连续也可不连续
        //val regex1 = "^(?=(.*[a-z]){2})(?=(.*[A-Z]){2})[a-zA-Z]{10}$" //可连续也可不连续

        //至少2个最多5个小写字母 至少2个最多5个大写字母 总共10个字母
        //val regex2 = "^(?=.*[a-z]{2,5})(?=.*[A-Z]{2,5})[a-zA-z]{10}$" //至少连续两个小写和大写字母
        val regex2 = "^(?=(.*[a-z]){2,5})(?=(.*[A-Z]){2,5})[a-zA-z]{10}$" //至少两个小写和大写字母 可连续或不可连续

        //任意字符3-10个
        //val regex3 = "^.{3,10}"

        //至少两个小写字母 两个大写字母 两个数字 总共10个字符
        val regex4 = "^(?=.*[a-z]{2})(?=.*[A-Z]{2})(?=.*\\d{2})[a-zA-Z0-9]{10}$" //连续两个小写或者大写字母或数字
        val regex5 = "^(?=(.*\\d){2})(?=(.*[a-z]){2})(?=(.*[A-Z]){2})[a-zA-Z0-9]{10}$" //可或不可连续 两个小写或者大写或数字

        val pattern = Pattern.compile(regex2)
        val matcher = pattern.matcher(content)
        println("matches:${matcher.matches()}")
    }

    @Test
    fun testMathNumber() {

        val regex = "^(-?0|([1-9]\\d*))\\.\\d+" //小数

        //val regex = "^(-?[1-9]\\d*)|0"  //整数

        //val regex = "^((-?[1-9]\\d*)|0)|(((-?[1-9]\\d*)|0)\\.\\d+)"

        //val content = "021"
        //val content = "21"
        //val content = "0"
        //val content = "1"
        //val content = "5999999999"
        //val content = "0000000"
        //val content = "10"

        //val content = "-0000000"
        //val content = "-021"
        //val content = "-21"
        //val content = "-0"
        //val content = "-1"
        //val content = "-5999999999"

        //val content = "1.5999999999"
        //val content = "-1.0"
        //val content = "-1.12"
        val content = "-0.0"
        //val content = "-0.12"

        val pattern = Pattern.compile(regex)
        val matcher = pattern.matcher(content)
        println("matches:${matcher.matches()}")
    }

    @Test
    fun regex0() {
        //?:非捕获分组
        val dataStr = "cot10 cot1111 cot90 cot110"
        val pattern = Pattern.compile("cot(?:10|11|90)")
        val matcher = pattern.matcher(dataStr)
        while (matcher.find()) {
            println("找到目标:" + matcher.group())
        }
    }

    @Test
    fun regex1() {
        //?=非捕获分组
        val dataStr = "cot12 cot1111 cot90 cot110"
        val pattern = Pattern.compile("cot(?=10|11|90)")
        val matcher = pattern.matcher(dataStr)
        while (matcher.find()) {
            println("找到目标:" + matcher.group())
        }
    }

    @Test
    fun regex2() {
        //?!非捕获分组
        val dataStr = "cot12 cot1111 cot90 cot110 cot18"
        val pattern = Pattern.compile("cot(?!10|11|90)")
        val matcher = pattern.matcher(dataStr)
        while (matcher.find()) {
            println("找到目标:" + matcher.group())
        }
    }


    @Test
    fun regex3() {
        //非贪婪捕获  只需满足一个条件即可
        val dataStr = "cot01111"
        val pattern = Pattern.compile("\\d+?")
        val matcher = pattern.matcher(dataStr)
        while (matcher.find()) {
            //matcher.group()与matcher.group(0)等价
            //println("找到目标:" + matcher.group())
            println("找到目标:" + matcher.group(0))
        }
    }

    @Test
    fun regex4() {
        //反向引用 寻找连续的两位数
        val dataStr = "cot11 da09 dab88"
        val pattern = Pattern.compile("(\\d)\\1")
        val matcher = pattern.matcher(dataStr)
        while (matcher.find()) {
            //matcher.group()与matcher.group(0)等价
            //println("找到目标:" + matcher.group())
            println("找到目标:" + matcher.group(0))
        }
    }

    @Test
    fun regex5() {
        //反向引用 寻找连续相同的五位数
        val dataStr = "cot11 da09 dab00000 dab88 io99999"
        //\\1表示方向引用第一个子表达式
        //{4}表示连续出现4次 加起前面出现的一次  总共五次
        val pattern = Pattern.compile("(\\d)\\1{4}")
        val matcher = pattern.matcher(dataStr)
        while (matcher.find()) {
            //matcher.group()与matcher.group(0)等价
            //println("找到目标:" + matcher.group())
            println("找到目标:" + matcher.group(0))
        }
    }

    @Test
    fun regex6() {
        //反向引用 寻找千位与各位相同,百位与十位相同的四位数
        val dataStr = "cot1221 da09 dab00000 dab88 io99999"
        //\\1表示方向引用第一个子表达式  \\2表示方向引用第二个子表达式
        //子表达从左往右数  依次为第一个  第二个 第三个......
        val pattern = Pattern.compile("(\\d)(\\d)\\2\\1")
        val matcher = pattern.matcher(dataStr)
        while (matcher.find()) {
            //matcher.group()与matcher.group(0)等价
            //println("找到目标:" + matcher.group())
            println("找到目标:" + matcher.group(0))
        }
    }

    @Test
    fun regex7() {
        //反向引用 寻找三个连续三通的三位数
        val dataStr = "da111222999 dab000456000 dab88 io666888333"
        //\\1表示方向引用第一个子表达式  \\2表示方向引用第二个子表达式
        //子表达从左往右数  依次为第一个  第二个 第三个......
        val pattern = Pattern.compile("(\\d)\\1{2}(\\d)\\2{2}(\\d)\\3{2}")
        val matcher = pattern.matcher(dataStr)
        while (matcher.find()) {
            //matcher.group()与matcher.group(0)等价
            //println("找到目标:" + matcher.group())
            println("找到目标:" + matcher.group(0))
        }
    }

    @Test
    fun regex8() {
        val dataStr = "da111222999 dab000456000 dab88 io666888333 da789"
        //连续递增
        val pattern0 = Pattern.compile("(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0))+\\d")
        //连续递减
        val pattern1 = Pattern.compile("(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)|0(?=9))+\\d")
        val matcher = pattern0.matcher(dataStr)
        while (matcher.find()) {
            //matcher.group()与matcher.group(0)等价
            //println("找到目标:" + matcher.group())
            println("找到目标:" + matcher.group(0))
        }
    }

    @Test
    fun regex9() {
        //相同的内容只保留一份
        val dataContent = "我我要学学学学java编程!"
        //.表示所有内容  $1表示匹配内容的第一个

        //反向引用:内部引用用\\  外部应用用$
        val newStr = Pattern.compile("(.)\\1+").matcher(dataContent).replaceAll("$1")
        println("newStr:${newStr}")
    }

    @Test
    fun regex10() {
        val dataStr = "问哦-大风尴尬的12负担啊发大水~发凡"
        val dataStrArr = dataStr.split(Regex("-|\\d+|~"))
        for (item in dataStrArr) {
            println("item:${item}")
        }
    }

    @Test
    fun regex11() {
        val dataList = mutableListOf<String>()
        val dataStr = "现在入群，更周边奖。"
        //中文和中文标点符号
        val characterRegex = "[\\u4e00-\\u9fa5]|[\\u3002\\uff1f\\uff01\\uff0c\\u3001\\uff1b\\uff1a\\u201c\\u201d\\u2018\\u2019\\uff08\\uff09\\u300a\\u300b\\u3008\\u3009\\u3010\\u3011\\u300e\\u300f\\u300c\\u300d\\ufe43\\ufe44\\u3014\\u3015\\u2026\\u2014\\uff5e\\ufe4f\\uffe5]"
        //val character = "\"[\\\\u3002\\\\uff1f\\\\uff01\\\\uff0c\\\\u3001\\\\uff1b\\\\uff1a\\\\u201c\\\\u201d\\\\u2018\\\\u2019\\\\uff08\\\\uff09\\\\u300a\\\\u300b\\\\u3008\\\\u3009\\\\u3010\\\\u3011\\\\u300e\\\\u300f\\\\u300c\\\\u300d\\\\ufe43\\\\ufe44\\\\u3014\\\\u3015\\\\u2026\\\\u2014\\\\uff5e\\\\ufe4f\\\\uffe5]\""
        val patter = Pattern.compile(characterRegex)
        val matcher = patter.matcher(dataStr)
        while (matcher.find()) {
            val item = matcher.group()
            dataList.add(item)
        }
        println("dataList.size--${dataList.size}")
    }
}
